package com.fymxy.question_bank_management.service.serviceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.mapper.ProblemMapper;
import com.fymxy.question_bank_management.pojo.Problem;
import com.fymxy.question_bank_management.pojo.ProblemWithBLOBs;
import com.fymxy.question_bank_management.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class ProblemServiceImpl implements ProblemService {

    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public int addProblem(ProblemWithBLOBs problem){
        return problemMapper.insert(problem);
    }

    @Override//返回所有题目并对题目内容长度超过9的缩减处理
    public IPage<ProblemWithBLOBs> selectAllProblem(Page page) {
        return subContent(problemMapper.selectAllProblem(page));
    }

    @Override
    public IPage<ProblemWithBLOBs> selectByKeyword(Page page,String keyword,String level,String type,String point) {
        keyword = '%'+ keyword.substring(1) + '%' ;

        level = '%'+level.substring(1)+'%' ;

        type = '%'+type.substring(1)+'%' ;

        point = '%'+point.substring(1)+'%' ;

        return subContent(problemMapper.selectByKeyword(page,keyword,level,type,point));
    }

    @Override
    public int deleteProblemById(Integer id) {
        return problemMapper.deleteProblemById(id);
    }

    @Override
    public int updateProblemById(ProblemWithBLOBs record) {
        return problemMapper.updateProblemById(record);
    }

    @Override
    public int getTopProblemNo(){
        return problemMapper.getTopProblemNo();
    }

    @Override
    public ProblemWithBLOBs selectById(Integer id) {
        return problemMapper.selectById(id);
    }

    @Override
    public List<ProblemWithBLOBs> selectByPaper(Integer paperId) {
        return problemMapper.selectByPaper(paperId);
    }

    @Override
    public List<ProblemWithBLOBs> selectProblemByPaper(Integer paperNo) {
        return problemMapper.selectByPaper(paperNo);
    }

    @Override
    public Problem selectByProblemNo(Integer problemNo) {
        return this.problemMapper.selectByProblemNo(problemNo);
    }

    @Override
    public ProblemWithBLOBs selectAllByProblemNo(Integer problemNo) {
        return problemMapper.selectAllByProblemNo(problemNo);
    }

    private IPage<ProblemWithBLOBs> subContent(IPage<ProblemWithBLOBs> problemWithBLOBsIPage){
        List<ProblemWithBLOBs> problemWithBLOBsList = problemWithBLOBsIPage.getRecords();
        for(int i =0 ;i<problemWithBLOBsList.size();i++){
            ProblemWithBLOBs problemWithBLOBs = problemWithBLOBsList.get(i);
            String subContent = problemWithBLOBs.getProblemContent();
            if(subContent.length()>9) {
                subContent = subContent.substring(0, 9) + "...";
                problemWithBLOBs.setProblemContent(subContent);
                problemWithBLOBsList.set(i, problemWithBLOBs);
            }
        }
        problemWithBLOBsIPage.setRecords(problemWithBLOBsList);
        return problemWithBLOBsIPage;
    }
}
